//
//  HWAddressSelectionViewController.m
//  OCDome
//
//  Created by 李含文 on 2020/8/31.
//  Copyright © 2020 李含文. All rights reserved.
//

#import "HWAddressSelectionViewController.h"


static HWAddressSelectionViewController *HWAddressSelectionVC;

@interface HWAddressSelectionViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, assign) NSInteger provinceIndex,citieIndex;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentView_buttom;

/* <#name#> */
@property (nonatomic, strong) HWAddressSelectionModel *model;

/* <#name#> */
@property (nonatomic, copy) void(^block)(HWAddressProvinceModel *province, HWAddressCityModel *city);
@end

@implementation HWAddressSelectionViewController


- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationOverCurrentContext;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
    
    [self loadData];
    
}

- (void)loadData {
    HWAddressSelectionModel *model = [HWAddressSelectionModel new];
    self.model = model;
    [self.pickerView reloadAllComponents];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.hidesBottomBarWhenPushed = NO;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView_buttom.constant = 0;
        [self.view layoutIfNeeded]; // 刷新fuk
    }];
}

- (IBAction)btnClick:(UIButton *)sender {
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView_buttom.constant = -280;
        [self.view layoutIfNeeded]; // 刷新fuk
    } completion:^(BOOL finished) {
        if (sender.tag == 101) {
            if (self.block) {
                self.block(self.model.provinces[self.provinceIndex], self.model.provinces[self.provinceIndex].cityList[self.citieIndex]);
            }
        }
        [self.view removeFromSuperview];
    }];
}

+ (void)show:(void(^)(HWAddressProvinceModel *province, HWAddressCityModel *city))action {
    UIWindow *window = nil;
    if (@available(iOS 13.0, *)) {
        window = [UIApplication sharedApplication].windows.firstObject;
    } else {
        window = [UIApplication sharedApplication].delegate.window;
    }
    if (!HWAddressSelectionVC) {
        HWAddressSelectionVC = [HWAddressSelectionViewController new];
    }
    HWAddressSelectionVC.block = action;
    HWAddressSelectionVC.view.frame = UIScreen.mainScreen.bounds;
    [window addSubview:HWAddressSelectionVC.view];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    switch (component) {
        case 0: {
            return self.model.provinces.count;
        };
        case 1: {
            HWAddressProvinceModel *province = self.model.provinces[self.provinceIndex];
            return province.cityList.count;
        }
        default: {
            return 0;
        }
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    switch (component) {
        case 0: {
            HWAddressProvinceModel *province = self.model.provinces[row];
            return province.name;
        }
        case 1: {
            HWAddressProvinceModel *province = self.model.provinces[self.provinceIndex];
            return province.cityList[row].name;
        }
        default: {
            return nil;
        }
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    switch (component) {
        case 0:{
            self.provinceIndex = row;
            self.citieIndex = 0;
            [pickerView reloadComponent:1];
            [pickerView selectRow:0 inComponent:1 animated:YES];
            break;
        }
        case 1:{
            self.citieIndex = row;
            break;
        }
        default:
            break;
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return [UIScreen mainScreen].bounds.size.width*0.5;
}
- (void)dealloc
{
    NSLog(@"释放");
}

@end
